package com.ip;

import java.sql.*;

public class TestJDBC {
    public static void main(String[] args) {
        //初始化驱动
        try {
            //驱动类com.mysql.jdbc.Driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            //建立与数据库得Connection连接
            //需要提供数据库所处得ip
            //数据库端口号
            //数据库名称
            String url = "jdbc:mysql://zuoda-dev.mysql.rds.aliyuncs.com/zuoda_db?&useSSL=false&serverTimezone=UTC";
            Connection c = DriverManager.getConnection(url,"zuoda_db_user","zuoda_123456");
            //Statement对象用于执行SQL语句
            Statement s = c.createStatement();
            String sql = "SELECT table_name FROM information_schema.tables limit 10";
            ResultSet resultSet = s.executeQuery(sql);
            System.out.println("连接成功，获取连接对象： " + s);
            //数据库连接是有限资源，相关操作结束后先关s再关c
            s.close();
            c.close();
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }
}
